<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>正则表达式 | emplay</title><meta name="keywords" content="python,正则表达式"><meta name="author" content="emplay"><meta name="copyright" content="emplay"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Python中的正则表达式">
<meta property="og:type" content="article">
<meta property="og:title" content="正则表达式">
<meta property="og:url" content="https://blog.emplay.top/2022/07/07/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/index.html">
<meta property="og:site_name" content="emplay">
<meta property="og:description" content="Python中的正则表达式">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="">
<meta property="article:published_time" content="2022-07-07T00:00:00.000Z">
<meta property="article:modified_time" content="2022-08-19T00:30:02.926Z">
<meta property="article:author" content="emplay">
<meta property="article:tag" content="python,正则表达式">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content=""><link rel="shortcut icon" href="/img/dbf.png"><link rel="canonical" href="https://blog.emplay.top/2022/07/07/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//fonts.googleapis.com" crossorigin=""/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Titillium+Web&amp;display=swap" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":true,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: true,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '正则表达式',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-08-19 00:30:02'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/iconfont/iconfont.css"><meta name="generator" content="Hexo 6.2.0"><link rel="alternate" href="/atom.xml" title="emplay" type="application/atom+xml">
</head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/emplay.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">2</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">1</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">2</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw iconfont icon-zhuye"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw iconfont icon-biaoqian"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw iconfont icon-suoyouwenzhang"></i><span> 文章总览</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 学以广才</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/%E7%90%A2%E7%8E%89%E6%88%90%E5%99%A8/"><i class="fa-fw iconfont icon-faxian_jiatingjiaoyu"></i><span> 琢玉成器</span></a></li><li><a class="site-page child" href="/categories/%E8%AF%BB%E5%8F%B2%E6%98%8E%E6%99%BA/"><i class="fa-fw iconfont icon-lishi"></i><span> 读史明智</span></a></li><li><a class="site-page child" href="/categories/%E6%89%8B%E5%B7%A5%E5%AE%9E%E9%AA%8C/"><i class="fa-fw iconfont icon-shiyan"></i><span> 手工实验</span></a></li><li><a class="site-page child" href="/categories/%E5%A4%A9%E9%AB%98%E5%9C%B0%E9%98%94/"><i class="fa-fw iconfont icon-lvyouxianlu"></i><span> 天高地阔</span></a></li><li><a class="site-page child" href="/categories/%E5%85%89%E5%BD%B1%E4%BC%A0%E5%A5%87/"><i class="fa-fw iconfont icon-sheying"></i><span> 光影传奇</span></a></li><li><a class="site-page child" href="/categories/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/"><i class="fa-fw iconfont icon-icon"></i><span> 读书笔记</span></a></li><li><a class="site-page child" href="/categories/%E5%90%83%E7%81%B0%E7%B3%BB%E5%88%97/"><i class="fa-fw iconfont icon-zaixianjiaocheng"></i><span> 吃灰系列</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 计算机相关</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/"><i class="fa-fw iconfont icon-qitacaozuoxitong"></i><span> 操作系统</span></a></li><li><a class="site-page child" href="/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/"><i class="fa-fw iconfont icon-shujuku"></i><span> 数据库</span></a></li><li><a class="site-page child" href="/categories/%E7%AE%97%E6%B3%95%E9%80%BB%E8%BE%91/"><i class="fa-fw iconfont icon-suanfaguanligongju"></i><span> 算法逻辑</span></a></li><li><a class="site-page child" href="/categories/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/"><i class="fa-fw iconfont icon-yuyan"></i><span> 编程语言</span></a></li><li><a class="site-page child" href="/categories/%E4%B8%8D%E6%83%B3%E4%BB%98%E9%92%B1/"><i class="fa-fw iconfont icon-pojiexiugai"></i><span> 不想付钱</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 怡性养神</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/%E7%94%B5%E5%BD%B1/"><i class="fa-fw iconfont icon-yingshi"></i><span> 电影</span></a></li><li><a class="site-page child" href="/categories/%E5%9B%BE%E5%BA%93/"><i class="fa-fw iconfont icon-24gf-photoAlbum"></i><span> 图库</span></a></li><li><a class="site-page child" href="/categories/%E9%9F%B3%E4%B9%90/"><i class="fa-fw iconfont icon-a-zujiantianchong_huaban1fuben17"></i><span> 音乐</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 链接</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="not-top-img" id="page-header"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">emplay</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw iconfont icon-zhuye"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw iconfont icon-biaoqian"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw iconfont icon-suoyouwenzhang"></i><span> 文章总览</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 学以广才</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/%E7%90%A2%E7%8E%89%E6%88%90%E5%99%A8/"><i class="fa-fw iconfont icon-faxian_jiatingjiaoyu"></i><span> 琢玉成器</span></a></li><li><a class="site-page child" href="/categories/%E8%AF%BB%E5%8F%B2%E6%98%8E%E6%99%BA/"><i class="fa-fw iconfont icon-lishi"></i><span> 读史明智</span></a></li><li><a class="site-page child" href="/categories/%E6%89%8B%E5%B7%A5%E5%AE%9E%E9%AA%8C/"><i class="fa-fw iconfont icon-shiyan"></i><span> 手工实验</span></a></li><li><a class="site-page child" href="/categories/%E5%A4%A9%E9%AB%98%E5%9C%B0%E9%98%94/"><i class="fa-fw iconfont icon-lvyouxianlu"></i><span> 天高地阔</span></a></li><li><a class="site-page child" href="/categories/%E5%85%89%E5%BD%B1%E4%BC%A0%E5%A5%87/"><i class="fa-fw iconfont icon-sheying"></i><span> 光影传奇</span></a></li><li><a class="site-page child" href="/categories/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/"><i class="fa-fw iconfont icon-icon"></i><span> 读书笔记</span></a></li><li><a class="site-page child" href="/categories/%E5%90%83%E7%81%B0%E7%B3%BB%E5%88%97/"><i class="fa-fw iconfont icon-zaixianjiaocheng"></i><span> 吃灰系列</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 计算机相关</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/"><i class="fa-fw iconfont icon-qitacaozuoxitong"></i><span> 操作系统</span></a></li><li><a class="site-page child" href="/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/"><i class="fa-fw iconfont icon-shujuku"></i><span> 数据库</span></a></li><li><a class="site-page child" href="/categories/%E7%AE%97%E6%B3%95%E9%80%BB%E8%BE%91/"><i class="fa-fw iconfont icon-suanfaguanligongju"></i><span> 算法逻辑</span></a></li><li><a class="site-page child" href="/categories/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/"><i class="fa-fw iconfont icon-yuyan"></i><span> 编程语言</span></a></li><li><a class="site-page child" href="/categories/%E4%B8%8D%E6%83%B3%E4%BB%98%E9%92%B1/"><i class="fa-fw iconfont icon-pojiexiugai"></i><span> 不想付钱</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 怡性养神</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/%E7%94%B5%E5%BD%B1/"><i class="fa-fw iconfont icon-yingshi"></i><span> 电影</span></a></li><li><a class="site-page child" href="/categories/%E5%9B%BE%E5%BA%93/"><i class="fa-fw iconfont icon-24gf-photoAlbum"></i><span> 图库</span></a></li><li><a class="site-page child" href="/categories/%E9%9F%B3%E4%B9%90/"><i class="fa-fw iconfont icon-a-zujiantianchong_huaban1fuben17"></i><span> 音乐</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 链接</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav></header><main class="layout" id="content-inner"><div id="post"><div id="post-info"><h1 class="post-title">正则表达式</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-07-07T00:00:00.000Z" title="发表于 2022-07-07 00:00:00">2022-07-07</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-08-19T00:30:02.926Z" title="更新于 2022-08-19 00:30:02">2022-08-19</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/">编程语言</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="正则表达式"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div><article class="post-content" id="article-container"><h2 id="正则表达式"><a href="#正则表达式" class="headerlink" title="正则表达式"></a>正则表达式</h2><h3 id="为什么使用正则"><a href="#为什么使用正则" class="headerlink" title="为什么使用正则"></a>为什么使用正则</h3><p>正则表达式为高级的文本模式匹配、抽取、与&#x2F;或文本形式的搜索和替换功能提供了基础。正则表达式是一些由字符和特殊符合组成的字符串，描述了模式的重复或表述多个字符。正则表达式能按照某种模式匹配一系列有相似特征的字符串。<br>Python通过标准库中的re模块来支持正则表达式。</p>
<h3 id="元字符"><a href="#元字符" class="headerlink" title="元字符"></a>元字符</h3><p>元字符是一系列特殊符号和字符，正是它给予正则表达式强大的功能和灵活性。<br>常见正则表达式和特殊符号</p>
<table>
<thead>
<tr>
<th align="left">元字符</th>
<th align="left">描述</th>
<th align="left">示例</th>
</tr>
</thead>
<tbody><tr>
<td align="left">literal</td>
<td align="left">匹配文本字符串的字面值literal</td>
<td align="left">foo</td>
</tr>
<tr>
<td align="left">re1|re2</td>
<td align="left">匹配正则表达式re1或re2</td>
<td align="left">foo|bar</td>
</tr>
<tr>
<td align="left">.</td>
<td align="left">匹配除\n外的任意字符</td>
<td align="left">a.b</td>
</tr>
<tr>
<td align="left">^</td>
<td align="left">匹配字符串起始部分</td>
<td align="left">^ab</td>
</tr>
<tr>
<td align="left">$</td>
<td align="left">匹配字符串终止部分</td>
<td align="left">ab$</td>
</tr>
<tr>
<td align="left">*</td>
<td align="left">匹配0次或多次前面出现的正则表达式</td>
<td align="left">[A-Z]*</td>
</tr>
<tr>
<td align="left">+</td>
<td align="left">匹配1次或多次前面出现的正则表达式</td>
<td align="left">[A-Z]+</td>
</tr>
<tr>
<td align="left">?</td>
<td align="left">匹配0次或1次前面出现的正则表达式</td>
<td align="left">ab?</td>
</tr>
<tr>
<td align="left">{N}</td>
<td align="left">匹配N次前面出现的正则表达式</td>
<td align="left">[0-9]{2}</td>
</tr>
<tr>
<td align="left">{M,N}</td>
<td align="left">匹配M~N次前面出现的正则表达式</td>
<td align="left">[0-9]{2,3}</td>
</tr>
<tr>
<td align="left">[a-z]</td>
<td align="left">匹配字符集中出现的任一字符</td>
<td align="left">[ab]</td>
</tr>
<tr>
<td align="left">[a-z]</td>
<td align="left">匹配a~z范围中的任一字符</td>
<td align="left">[a-z]</td>
</tr>
<tr>
<td align="left">[^ab]</td>
<td align="left">不匹配字符集中的字符，可以使用范围字符</td>
<td align="left">[^ab]</td>
</tr>
<tr>
<td align="left">(*|+|?|{})?</td>
<td align="left">用于匹配上面频繁出现、重复出现符号的非贪婪版本（*、+、？、{}）</td>
<td align="left">.*?[a-z]</td>
</tr>
<tr>
<td align="left">(…)</td>
<td align="left">匹配封闭的正则表达式，然后另存为子组</td>
<td align="left">([0-9]{3})？,f(oo</td>
</tr>
<tr>
<td align="left">\d</td>
<td align="left">匹配十进制数字，相当于[0-9]，与\D相反</td>
<td align="left">ab\d</td>
</tr>
<tr>
<td align="left">\w</td>
<td align="left">匹配任意字母数字，相当于[A-Za-Z0-9]，与、\W相反</td>
<td align="left">[A-Za-z_]\w</td>
</tr>
<tr>
<td align="left">\s</td>
<td align="left">匹配任意空格字符，相当于[\n\t\r\v\f]，与\S相反</td>
<td align="left">of\sthe</td>
</tr>
<tr>
<td align="left">\b</td>
<td align="left">匹配单词边界，与\B相反</td>
<td align="left">\bThe\b</td>
</tr>
<tr>
<td align="left">\N</td>
<td align="left">匹配以保存的子组N，参见(…)</td>
<td align="left">ab:\2</td>
</tr>
<tr>
<td align="left">\c</td>
<td align="left">逐字匹配任何特殊字符c，斜线是转义</td>
<td align="left">,</td>
</tr>
<tr>
<td align="left">\A(\Z)</td>
<td align="left">匹配字符串的起始（结束）</td>
<td align="left">\Acd</td>
</tr>
<tr>
<td align="left">(?iLmsux)</td>
<td align="left">在正则表达式中嵌入一个或多个特殊“标记”参数</td>
<td align="left">(?x),(? im)</td>
</tr>
<tr>
<td align="left">(?:…)</td>
<td align="left">表示一个匹配不用保存的分组</td>
<td align="left">(?:\w+\.)*</td>
</tr>
<tr>
<td align="left">(?P<name>…)</td>
<td align="left">像一个仅由name标识而不是数字ID标识的正则分组匹配</td>
<td align="left">(?P<data>)</td>
</tr>
<tr>
<td align="left">(?P&#x3D;name)</td>
<td align="left">在同一字符串中匹配由(?P&lt;\name)分组的之前文本</td>
<td align="left">(?P&#x3D;data)</td>
</tr>
<tr>
<td align="left">(?#…)</td>
<td align="left">表示注释，所有内容都被忽略</td>
<td align="left">(?#comment)</td>
</tr>
<tr>
<td align="left">(?&#x3D;…)</td>
<td align="left">匹配条件是如果…出现在之后的位置，而不使用输入字符串；称作正向前视断言</td>
<td align="left">(?&#x3D;.com)</td>
</tr>
<tr>
<td align="left">(?!…)</td>
<td align="left">匹配条件是如果…不出现在之后的位置，而不使用输入字符串；称作负向前视断言</td>
<td align="left">(?!.net)</td>
</tr>
<tr>
<td align="left">(?&lt;&#x3D;…)</td>
<td align="left">匹配条件是如果…出现在之前的位置，而不使用输入字符串；称作正向后视断言</td>
<td align="left">(?&lt;&#x3D;800-)</td>
</tr>
<tr>
<td align="left">(?&lt;!…)</td>
<td align="left">匹配条件是如果…不出现在之前的位置，而不使用输入字符串；称作负向后视断言</td>
<td align="left">(?&lt;!192\.168\.)</td>
</tr>
<tr>
<td align="left">(?(id&#x2F;name)Y|N)</td>
<td align="left">如果分组所提供的id或者name（名称）存在，就返回正则表达式的条件匹配Y，如果不存在，就返回N；</td>
<td align="left">N是可选项</td>
</tr>
</tbody></table>
<h3 id="使用择一符号匹配多个正则表达式模式"><a href="#使用择一符号匹配多个正则表达式模式" class="headerlink" title="使用择一符号匹配多个正则表达式模式"></a>使用择一符号匹配多个正则表达式模式</h3><p>表示择一匹配的是管道符(|)，用于分割不同的正则表达式。也称作并（union）、逻辑或（OR）</p>
<h3 id="使用闭包操作符是想存在性和频数匹配"><a href="#使用闭包操作符是想存在性和频数匹配" class="headerlink" title="使用闭包操作符是想存在性和频数匹配"></a>使用闭包操作符是想存在性和频数匹配</h3><p>星号将匹配其左边的正则表达式出现零次或多次的情况，该操作成为Kleene闭包；加号将匹配一次货多次出现的正则表达式，称为正闭包操作符。其他如问号匹配零次或一次，大括号匹配N次或一个范围。<br>如果问号紧跟在任何使用闭合操作符的匹配后面，将匹配尽可能少的次数，非贪婪匹配。<br>当模式匹配使用分组操作符时，正则表达式引擎将试图“吸收”匹配该模式的尽可能多的字符。这通常被叫做贪婪匹配。</p>
<table>
<thead>
<tr>
<th align="left">正则表达式</th>
<th align="left">匹配字符串</th>
</tr>
</thead>
<tbody><tr>
<td align="left">[dn]ot?</td>
<td align="left">字母“d”或者“n”，后面跟着一个“o”，然后是最多一个“t”，例如，do、no、dot、not</td>
</tr>
<tr>
<td align="left">0?[1-9]</td>
<td align="left">任何数值数字，它可能前置一个“0”，例如，匹配一系列数（表示从1～9月的数值），不管是一个还是两个数字</td>
</tr>
<tr>
<td align="left">[0-9]{15,16}</td>
<td align="left">匹配15或者16个数字（例如信用卡号码）</td>
</tr>
<tr>
<td align="left">&lt;&#x2F;?[^&gt;]+&gt;</td>
<td align="left">匹配全部有效的（和无效的）HTML标签</td>
</tr>
<tr>
<td align="left">[KQRBNP][a-h][1-8]-[a-h][1-8]</td>
<td align="left">在“长代数”标记法中，表示国际象棋合法的棋盘移动（仅移动，不包括吃子和将军）。即“K”、“Q”、“R”、“B”、“N”或“P”等字母后面加上“a1”～“h8”之间的棋盘坐标。前面的坐标表示从哪里开始走棋，后面的坐标代表走到哪个位置（棋格）上</td>
</tr>
</tbody></table>
<h3 id="使用圆括号指定分组"><a href="#使用圆括号指定分组" class="headerlink" title="使用圆括号指定分组"></a>使用圆括号指定分组</h3><p>圆括号功能</p>
<ul>
<li>对正则表达式进行分组</li>
<li>匹配子组</li>
</ul>
<table>
<thead>
<tr>
<th align="left">正则表达式</th>
<th align="left">匹配字符串</th>
</tr>
</thead>
<tbody><tr>
<td align="left">\d+(.\d*)?</td>
<td align="left">表示简单浮点数的字符串；也就是说，任何十进制数字，后面可以接一个小数点和零个或者多个十进制数字，例如“0.004”、“2”、“75.”等</td>
</tr>
<tr>
<td align="left">(Mr?s?.)?[A-Z][a-z]*[A-Za-z-]+</td>
<td align="left">名字和姓氏，以及对名字的限制（如果有，首字母必须大写，后续字母小写），全名前可以有可选的“Mr.”、“Mrs.”、“Ms.”或者“M.”作为称谓，以及灵活可选的姓氏，可以有多个单词、横线以及大写字母</td>
</tr>
</tbody></table>
<h3 id="扩展"><a href="#扩展" class="headerlink" title="扩展"></a>扩展</h3><table>
<thead>
<tr>
<th align="left">正则表达式</th>
<th align="left">匹配字符串</th>
</tr>
</thead>
<tbody><tr>
<td align="left">(?:\w+.)*</td>
<td align="left">以句点作为结尾的字符串，例如“google.”、“twitter.”、“facebook.”，但是这些匹配不会保存下来供后续的使用和数据检索</td>
</tr>
<tr>
<td align="left">(?#comment)</td>
<td align="left">此处并不做匹配，只是作为注释</td>
</tr>
<tr>
<td align="left">(?&#x3D;.com)</td>
<td align="left">如果一个字符串后面跟着“.com”才做匹配操作，并不使用任何目标字符串</td>
</tr>
<tr>
<td align="left">(?!.net)</td>
<td align="left">如果一个字符串后面不是跟着“.net”才做匹配操作</td>
</tr>
<tr>
<td align="left">(?&lt;&#x3D;800-)</td>
<td align="left">如果字符串之前为“800-”才做匹配，假定为电话号码，同样，并不使用任何输入字符串</td>
</tr>
<tr>
<td align="left">(?&lt;!192.168.)</td>
<td align="left">如果一个字符串之前不是“192.168.”才做匹配操作，假定用于过滤掉一组C类IP地址</td>
</tr>
<tr>
<td align="left">(?(1)y|x)</td>
<td align="left">如果一个匹配组1（\1）存在，就与y匹配；否则，就与x匹配</td>
</tr>
</tbody></table>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://blog.emplay.top">emplay</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://blog.emplay.top/2022/07/07/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/">https://blog.emplay.top/2022/07/07/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://blog.emplay.top" target="_blank">emplay</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/python-%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/">python,正则表达式</a></div><div class="post_share"><div class="social-share" data-image="" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="next-post pull-full"><a href="/2022/07/20/%E4%BD%BF%E7%94%A8dbt5%E8%BF%9B%E8%A1%8CTPC-E%E6%B5%8B%E8%AF%95/"><img class="next-cover" src="" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">使用dbt5进行TPC-E测试</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/emplay.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">emplay</div><div class="author-info__description">博学之，审问之，慎思之，明辨之，笃行之</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">2</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">1</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">2</div></a></div><a id="card-info-btn" href="https://blog.emplay.top/"><i class="fab fa-github"></i><span>返回主页</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://gitee.com/emplay/emplay" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:871586557@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="toc-text">正则表达式</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BA%E4%BB%80%E4%B9%88%E4%BD%BF%E7%94%A8%E6%AD%A3%E5%88%99"><span class="toc-text">为什么使用正则</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%83%E5%AD%97%E7%AC%A6"><span class="toc-text">元字符</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E6%8B%A9%E4%B8%80%E7%AC%A6%E5%8F%B7%E5%8C%B9%E9%85%8D%E5%A4%9A%E4%B8%AA%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E6%A8%A1%E5%BC%8F"><span class="toc-text">使用择一符号匹配多个正则表达式模式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E9%97%AD%E5%8C%85%E6%93%8D%E4%BD%9C%E7%AC%A6%E6%98%AF%E6%83%B3%E5%AD%98%E5%9C%A8%E6%80%A7%E5%92%8C%E9%A2%91%E6%95%B0%E5%8C%B9%E9%85%8D"><span class="toc-text">使用闭包操作符是想存在性和频数匹配</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E5%9C%86%E6%8B%AC%E5%8F%B7%E6%8C%87%E5%AE%9A%E5%88%86%E7%BB%84"><span class="toc-text">使用圆括号指定分组</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%89%A9%E5%B1%95"><span class="toc-text">扩展</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2022/07/20/%E4%BD%BF%E7%94%A8dbt5%E8%BF%9B%E8%A1%8CTPC-E%E6%B5%8B%E8%AF%95/" title="使用dbt5进行TPC-E测试"><img src="" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="使用dbt5进行TPC-E测试"/></a><div class="content"><a class="title" href="/2022/07/20/%E4%BD%BF%E7%94%A8dbt5%E8%BF%9B%E8%A1%8CTPC-E%E6%B5%8B%E8%AF%95/" title="使用dbt5进行TPC-E测试">使用dbt5进行TPC-E测试</a><time datetime="2022-07-20T00:00:00.000Z" title="发表于 2022-07-20 00:00:00">2022-07-20</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/07/07/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/" title="正则表达式"><img src="" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="正则表达式"/></a><div class="content"><a class="title" href="/2022/07/07/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/" title="正则表达式">正则表达式</a><time datetime="2022-07-07T00:00:00.000Z" title="发表于 2022-07-07 00:00:00">2022-07-07</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2022 By emplay</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="/js/search/local-search.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>